package frame;

import main.Resource;
import other.MouseMoveAnim;

import javax.swing.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

public abstract class FrameBase {
    protected JFrame frame;
    protected MouseMoveAnim moveAnim;

    public FrameBase() {
        frame = new JFrame();
        if (Resource.Config.useMouseMoveAnim) {
            moveAnim = new MouseMoveAnim(frame);
            frame.getLayeredPane().add(moveAnim);
        }
    }

    /**
     * 自适应面板
     */
    protected abstract void selfAdaption();

    protected void iniFrameListener() {
        frame.addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e) {
                selfAdaption();
            }

            @Override
            public void componentMoved(ComponentEvent e) {

            }

            @Override
            public void componentShown(ComponentEvent e) {

            }

            @Override
            public void componentHidden(ComponentEvent e) {

            }
        });
        frame.addWindowStateListener(new WindowStateListener() {
            @Override
            public void windowStateChanged(WindowEvent e) {
                selfAdaption();
            }
        });
    }

    protected void destroy() {
        if (moveAnim != null) {
            moveAnim.destroy();
        }
        frame.dispose();
    }
}
